package com.bz.xtcx.manager.controller;

import com.bz.xtcx.manager.entity.BusUser;
import com.bz.xtcx.manager.service.ISysUserService;
import com.bz.xtcx.manager.utils.WzStrUtil;
import com.bz.xtcx.manager.vo.VoPwd;
import com.bz.xtcx.manager.vo.VoResponse;
import com.bz.xtcx.manager.vo.VoUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("user")
public class LoginController extends BaseController{
    
    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value = "登录", httpMethod = "POST", notes = "登录求")
    @PostMapping("login")
    public Object login(@RequestBody VoUser vo) {
        VoResponse voRes = getVoResponse();
        if(StringUtils.isEmpty(vo.getUserName()) || StringUtils.isEmpty(vo.getPassword())) return voRes;
        voRes = sysUserService.signIn(vo.getUserName(), vo.getPassword(), vo.isAdmin());
        return voRes;
    }

    /**
     * 找回密码
     * @param vo
     * @return
     */
    @PostMapping("lookPwd")
    public Object lookPassword(@RequestBody VoPwd vo) {
        VoResponse voRes = getVoResponse();
        if(StringUtils.isEmpty(vo.getCode())) {
            voRes.setData("验证码不能为空");
            return voRes;
        }
        if(StringUtils.isEmpty(vo.getNewPassword())) {
            voRes.setFail(voRes);
            voRes.setData("密码不能为空");
            return voRes;
        }
        voRes = sysUserService.lookUserPwd(vo);
        return voRes;
    }

    /**
     * 超级管理员重置注册用户密码
     * @param vo
     * @return
     */
    @PostMapping("setDefPwd")
    public VoResponse resetUserPwd(@RequestBody VoPwd vo) {
        VoResponse voRes = getVoResponse();
        if(StringUtils.isEmpty(vo.getId())) {
            voRes.setData("待重置账号信息不能为空");
            return voRes;
        }
        voRes = sysUserService.setDefUserPwd(vo);
        return voRes;
    }

    /**
     * 管理员找回密码
     * @param vo
     * @return
     */
    @PostMapping("lookAdminPwd")
    public VoResponse lookAdminPassword(@RequestBody VoPwd vo) {
        VoResponse voRes = getVoResponse();
        if (WzStrUtil.isBlank(vo.getUserName())) {
            voRes.setFail(voRes);
            voRes.setMessage("账号不能为空");
        }
        if (WzStrUtil.isBlank(vo.getCellphone())) {
            voRes.setFail(voRes);
            voRes.setMessage("手机号码不能为空");
        }
        if(WzStrUtil.isBlank(vo.getCode())) {
            voRes.setFail(voRes);
            voRes.setData("验证码不能为空");
            return voRes;
        }
        if(WzStrUtil.isBlank(vo.getNewPassword())) {
            voRes.setFail(voRes);
            voRes.setData("密码不能为空");
            return voRes;
        }
        voRes = sysUserService.lookAdminPwd(vo);
        return voRes;
    }

    @PostMapping("sendCellphoneCode")
    public Object sendCellphoneCode(@RequestBody VoPwd vo){
//        VoResponse voRes = new VoResponse();
        /*BusUser su = sysUserService.getUserByCellPhone(vo.getCellphone());
        if (null == su) {
            voRes.setFail(voRes);
            voRes.setMessage("未查询到该手机号码，请尝试其他重置方式");
            return voRes;
        }*/
        VoResponse voRes = sysUserService.sendCellphoneCode(vo.getCellphone());
//        if(!) {
//            voRes.setFail(voRes);
//            voRes.setMessage("短信验证码发送失败");
//        }
        return voRes;
    }

    @PostMapping("sendEmailCode")
    public Object sendEmailCode(@RequestBody VoPwd vo){
//        VoResponse voRes = new VoResponse();
        /*BusUser su = sysUserService.getUserByEmail(vo.getEmail());
        if (null == su) {
            voRes.setFail(voRes);
            voRes.setMessage("未查询到该邮箱地址，请尝试其他重置方式");
            return voRes;
        }*/
        VoResponse voRes = sysUserService.sendEmailCode(vo.getEmail());
//        if(!sysUserService.sendEmailCode(vo.getEmail())) {
//            voRes.setFail(voRes);
//            voRes.setMessage("邮件验证码发送失败");
//        }
        return voRes;
    }

    @PostMapping("sendEmailActivate")
    public Object sendEmailActivate(@RequestBody VoPwd vo){
        VoResponse voRes = new VoResponse();
        BusUser user = sysUserService.getUserByEmail(vo.getEmail());
        if(user != null && user.getCheckStatus() == 0) {//未激活
        	if(!sysUserService.sendEmailActivate(vo.getEmail())) {
                voRes.setFail(voRes);
                voRes.setMessage("邮件发送失败，请检查邮箱");
            }else {
            	voRes.setMessage("激活邮件已经发出；请查收");
            }
        }else {
        	voRes.setFail(voRes);
        	voRes.setMessage("账号已激活成功；请返回登录");
        }
        return voRes;
    }

    /**
     * 修改密码
     * @param vo
     * @return
     */
    @PostMapping("resetPwd")
    public Object resetPassword(@RequestBody VoPwd vo) {
        VoResponse voRes = getVoResponse();
        if(StringUtils.isEmpty(vo.getNewPassword())) {
            voRes.setFail(voRes);
            voRes.setData("密码不能为空");
            return voRes;
        }
        voRes = sysUserService.updateUserPwd(vo);
        return voRes;
    }
    
    /**
     * 获取用户资料
     * @return
     */
    @GetMapping
    public Object getUser(){
        VoResponse voRes = new VoResponse();
        voRes.setData(sysUserService.getUserInfo());
        return voRes;
    }
    
    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping
    public Object updateBusUser(@RequestBody VoPwd user) {
        VoResponse voRes =  new VoResponse();
        if(StringUtils.isEmpty(user.getId())) {
            voRes.setFail(voRes);
            voRes.setMessage("参数错误");
        }
        voRes = sysUserService.updateUser(user);
        return voRes;
    }
    
    
    @GetMapping("logout")
    public Object logout(){
        VoResponse voRes = new VoResponse();
        sysUserService.signOut();
        return voRes;
    }

    @ApiOperation(value = "获得菜单", httpMethod = "POST", notes = "获得菜单")
    @GetMapping("getMenus")
    public Object getCurrentUserMenus(){
        VoResponse voRes = new VoResponse();
        voRes.setData(sysUserService.getCurrentUserMenus());
        return voRes;
    }

    @PostMapping("register")
    public Object register(@RequestBody BusUser user) {
        VoResponse voRes = getVoResponse();
        if(StringUtils.isEmpty(user.getEmail())) {
            voRes.setNull(voRes);
            voRes.setMessage("邮箱地址不能为空");
            return voRes;
        }
        if(StringUtils.isEmpty(user.getPassword())) {
            voRes.setNull(voRes);
            voRes.setMessage("密码不能为空");
            return voRes;
        }
        if(user.getUserType() <= 0 || user.getUserType() >= 6) {
            voRes.setNull(voRes);
            voRes.setMessage("用户类型异常");
            return voRes;
        }
        if(user.getAgreement() != 1) {
            voRes.setNull(voRes);
            voRes.setMessage("请仔细阅读平台服务协议");
            return voRes;
        }
        voRes = sysUserService.register(user);
        return voRes;
    }
    
    @GetMapping("activate")
    public Object activate(@RequestParam("activateId") String id){
        VoResponse voRes = sysUserService.activate(id);
        return voRes;
    }
    
    @GetMapping("getRedisUser")
    public Object getRedisUser(@RequestParam("userId") String id){
        return sysUserService.getRedisUser(id);
    }
    
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        //对密码进行 md5 加密
        String password = "123456";
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(md5Password);
    }
    
}
